package com.chief.lesson1;


import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class StreamWordCountSocket {

    public static void main(String[] args) throws Exception {
        ParameterTool parameterTool = ParameterTool.fromArgs(args);
        String host = parameterTool.get("host");
        int port = parameterTool.getInt("port");

        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> hadoop52 = environment.socketTextStream(host, port);

        SingleOutputStreamOperator<Tuple2<String, Integer>> sum = hadoop52.flatMap(new WordCount.MyFlatMapFunction()).setParallelism(2)
                .keyBy(value -> value.f0)
                .sum(1).setParallelism(2);

        sum.print().setParallelism(1);

        environment.execute();

    }
}
